package ch.wanandroid.lib.ui.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ch.wanandroid.comr.lib.R;
import ch.wanandroid.comr.lib.R2;
import ch.wanandroid.lib.util.CollectionUtil;

/**
 * @author wulinpeng
 * @datetime: 18/9/17 下午11:52
 * @description:
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Unbinder mUnBinder;
    public String df;
//    @BindView(R2.id.tool_bar)
//    public Toolbar mToolbar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!initArgs(savedInstanceState)) {
            finish();
            return;
        }
        initArgs(savedInstanceState);
        setContentView(getLayoutId());
        initView();
        initData();
    }

    /**
     * 初始化参数
     * @return 参数正确返回true，否则返回false
     */
    protected boolean initArgs(Bundle savedInstanceState) {
        return true;
    }

    protected void initView() {
        mUnBinder = ButterKnife.bind(this);
//        setSupportActionBar(mToolbar);
//        if (getSupportActionBar() != null) {
//            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//            getSupportActionBar().setDisplayShowTitleEnabled(false);
//        }
    }

    protected void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnBinder.unbind();
    }

    protected abstract int getLayoutId();

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    @Override
    public void onBackPressed() {
        List<Fragment> fragmentList = getSupportFragmentManager().getFragments();
        if (!CollectionUtil.isEmpty(fragmentList)) {
            for (Fragment fragment : fragmentList) {
                if (fragment instanceof BaseFragment && ((BaseFragment) fragment).onBackPressed()) {
                    // fragment处理了返回事件
                    return;
                }
            }
        }
        super.onBackPressed();
        finish();
    }
}
